package com.hifar.test.unitclassify.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hifar.fw.utils.id.IdUtils;
import com.hifar.plat.officUtil.ExcelTemplateUtil;
import com.hifar.sys.BusiResponse;
import com.hifar.sys.RequestConver;
import com.hifar.test.unitclassify.mapper.HfPrjUnitClassifyMapper;
import com.hifar.test.unitclassify.pojo.HfPrjUnitClassify;
import com.hifar.test.unitclassify.pojo.HfPrjUnitClassifyAndUnitVo;
import com.hifar.test.unitclassify.pojo.VO.HfPrjUnitClassifyVO;
import com.hifar.test.unitclassify.service.IHfPrjUnitClassifyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 项目-检测项目-分类 服务实现类
 * </p>
 *
 * @author wangkepeng
 * @since 2024-12-03 18:06:03
 */
@Service
public class HfPrjUnitClassifyServiceImpl extends ServiceImpl<HfPrjUnitClassifyMapper, HfPrjUnitClassify>
        implements IHfPrjUnitClassifyService {

    @Autowired
    HfPrjUnitClassifyMapper hfPrjUnitClassifyMapper;

    @Override
    public BusiResponse exportExcel(Map<String, Object> pageInfo) {
        BusiResponse busiResponse = new BusiResponse();
        String ids = (String) pageInfo.get("ids");

        String operationType = (String) pageInfo.get("operationType");

        if (StringUtils.isNotBlank(operationType) && (operationType.equals("template"))) {
            ExcelTemplateUtil.exportExcelTemplate(busiResponse, HfPrjUnitClassify.class, "导入模板", "项目-检测项目-分类导入数据");
            return busiResponse;
        } else {
            QueryWrapper<HfPrjUnitClassify> postParam = RequestConver.getPostParam(pageInfo);
            postParam.ne("del_flag", 1);
            postParam.orderByDesc("create_time");
            if (StringUtils.isNotBlank(ids)) {
                postParam.in("id", ids);
            }
            List<HfPrjUnitClassify> dataList = this.list(postParam);
            if (dataList != null && dataList.size() > 0) {
                ExcelTemplateUtil.exportExcel(busiResponse, dataList, HfPrjUnitClassify.class, "项目-检测项目-分类数据",
                        "项目-检测项目-分类导出");
            } else {
                ExcelTemplateUtil.exportExcelTemplate(busiResponse, HfPrjUnitClassify.class, "项目-检测项目-分类数据",
                        "项目-检测项目-分类导出");
            }
            return busiResponse;
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public BusiResponse importExcel(MultipartFile file) throws Exception {
        if (file != null && !file.isEmpty()) {
            List<HfPrjUnitClassify> importExcel = ExcelTemplateUtil.importExcel(file.getInputStream(),
                    HfPrjUnitClassify.class);
            List<HfPrjUnitClassify> importExcelList = new ArrayList<HfPrjUnitClassify>();
            if (importExcel != null && importExcel.size() > 0) {
                for (HfPrjUnitClassify bean : importExcel) {
                    bean.setId(IdUtils.uuid32());
//					bean.setDelFlag(0);
                    // if(!bean.getName().equals("")) {
                    importExcelList.add(bean);
                    // }
                }
                this.saveBatch(importExcelList);
                return BusiResponse.ok("导入成功！");
            }
        }
        return BusiResponse.error("导入失败，原因未知！");
    }

    @Override
    public Page<Map<String, Object>> selectTestMethod(Page<Map<String, Object>> rowPage, String sqlString) {
        return this.getBaseMapper().selectTestMethod(rowPage, sqlString);
    }

    @Override
    public Page<Map<String, Object>> selectTestQueryWrapper(Page<Map<String, Object>> rowPage, QueryWrapper<HfPrjUnitClassify> wrapper) {
        return this.getBaseMapper().selectTestQueryWrapper(rowPage, wrapper);
    }

    @Override
    public Page<HfPrjUnitClassify> selectTestQueryWrapperObj(Page<HfPrjUnitClassify> rowPage, QueryWrapper<HfPrjUnitClassify> wrapper) {
        return this.getBaseMapper().selectTestQueryWrapperObj(rowPage, wrapper);
    }

    @Override
    public Page<HfPrjUnitClassify> selectTestQueryWrapperObj4(Page<HfPrjUnitClassify> rowPage, QueryWrapper<HfPrjUnitClassify> wrapper) {
        return this.getBaseMapper().selectTestQueryWrapperObj4(rowPage, wrapper);
    }

    @Override
    public Page<HfPrjUnitClassifyVO> selectTestQueryWrapperObj5(Page<HfPrjUnitClassifyVO> rowPage, QueryWrapper<HfPrjUnitClassifyVO> wrapper) {
        return this.getBaseMapper().selectTestQueryWrapperObj5(rowPage, wrapper);
    }

    public void testmethod() {

    }

    @Override
    public List<HfPrjUnitClassifyAndUnitVo> listHfPrjUnitClassifyAndUnitVo(QueryWrapper<HfPrjUnitClassifyAndUnitVo> wrapper) {
        return this.getBaseMapper().listHfPrjUnitClassifyAndUnitVo(wrapper);
    }

}
